package com.itheima.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.itheima.controller.EasyExcelFactoryController;
import com.itheima.domain.TContractListExcel;
import com.itheima.service.LoadService;
import com.itheima.util.FullcarbillTraceThreadPoolUtil;
import org.apache.commons.collections4.ListUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author chenry
 * @Package com.itheima.service.impl
 * @Date 2025/2/23 18:06
 */
@Service
public class LoadServiceImpl implements LoadService {
//    @Override
//    public void policyImport(List<List<Object>> list, int successCount, int failureCount) {
//
//    }

    private static final Logger logger = LoggerFactory.getLogger(EasyExcelFactoryController.class);

    @Transactional(value = "transactionManager", timeout = 600)
    public void policyImport(List<List<Object>> listob, final int successCountt) {

        // 商品集合
        List<TContractListExcel> policyGoodAll = new ArrayList<>();

        try {
            // 政策集合
            List<TContractListExcel> contractLists = buildContractList(listob);

            int batchSize = 5000;
            int contractListOfBatches = (contractLists.size() + batchSize - 1) / batchSize;

            int policyGoodOfBatches = (policyGoodAll.size() + batchSize - 1) / batchSize;
            CountDownLatch latch = new CountDownLatch(contractListOfBatches + policyGoodOfBatches);

            AtomicInteger successCount = new AtomicInteger(0);

            for (int i = 0; i < contractListOfBatches; i++) {

                int start = i * batchSize;
                int end = Math.min(start + batchSize, contractLists.size());
                List<TContractListExcel> contractListSubs = contractLists.subList(start, end);
                FullcarbillTraceThreadPoolUtil.getWlhyThreadPoolExecutor().submit(() -> {
                    List<List<TContractListExcel>> contractListAll = ListUtils.partition(contractListSubs, 10);
                    for (List<TContractListExcel> tContractLists : contractListAll) {

//                        contractListMapper.insertContractList(tContractLists);

                        successCount.addAndGet(tContractLists.size());

                    }
                    latch.countDown();
                });
            }
            latch.await();

            // 插入记录表
//            insertImportRecord(contractLists, loginCodes, successCount);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("Batch insert.ERROR:{}",e.getMessage());
        }
    }

    private List<TContractListExcel> buildContractList(List<List<Object>> listob) {
        List<TContractListExcel> TContractLists = new ArrayList<>();
        for (List<Object> objects : listob) {
            TContractListExcel contractList = new TContractListExcel();
            if (objects.get(0) != null && StringUtils.isNotBlank(objects.get(0).toString())) {
                contractList.setA(objects.get(0).toString());
            }
            if (objects.get(1) != null && StringUtils.isNotBlank(objects.get(1).toString())) {
                contractList.setB(objects.get(1).toString());
            }
            if (objects.get(2) != null && StringUtils.isNotBlank(objects.get(2).toString())) {
                contractList.setC(objects.get(2).toString());
            }
            if (objects.get(3) != null && StringUtils.isNotBlank(objects.get(3).toString())) {
                contractList.setD(objects.get(3).toString());
            }
            if (objects.get(4) != null && StringUtils.isNotBlank(objects.get(4).toString())) {
                contractList.setF(objects.get(4).toString());
            }
            if (objects.get(5) != null && StringUtils.isNotBlank(objects.get(5).toString())) {
                // 主产品id
                contractList.setE(objects.get(5).toString());
            }
            if (objects.get(6) != null && StringUtils.isNotBlank(objects.get(6).toString())) {
                contractList.setG(objects.get(6).toString());
            }

            // 附加产品月底失效
            if (objects.get(7) != null && StringUtils.isNotBlank(objects.get(7).toString())) {
                contractList.setH(objects.get(7).toString());
            }
            if (objects.get(8) != null && StringUtils.isNotBlank(objects.get(7).toString())) {
                contractList.setI(objects.get(8).toString());
            }
            if (objects.get(9) != null && StringUtils.isNotBlank(objects.get(7).toString())) {
                contractList.setJ(objects.get(9).toString());
            }
            if (objects.get(11) != null && StringUtils.isNotBlank(objects.get(7).toString())) {
                contractList.setK(objects.get(11).toString());
            }
            if (objects.get(12) != null && StringUtils.isNotBlank(objects.get(7).toString())) {
                contractList.setL(objects.get(12).toString());
            }
            if (objects.get(13) != null && StringUtils.isNotBlank(objects.get(7).toString())) {
                contractList.setM(objects.get(13).toString());
            }
            if (objects.get(14) != null && StringUtils.isNotBlank(objects.get(7).toString())) {
                contractList.setN(objects.get(14).toString());
            }

            if (objects.get(21) != null && StringUtils.isNotBlank(objects.get(7).toString())) {
                contractList.setV(objects.get(21).toString());
            }
            TContractLists.add(contractList);
        }
        return TContractLists;
    }

}
